package stack_and_queue;

import java.util.Stack;

public class TwoStacksQueue {

    private Stack<Integer> stackPush;
    private Stack<Integer> stackPop;

    public TwoStacksQueue() {
        this.stackPush = new Stack<Integer>();
        this.stackPop = new Stack<Integer>();
    }

    public void pushToPop(){
        if(this.stackPop.isEmpty()){
            while(!this.stackPush.isEmpty()){
                this.stackPop.push(this.stackPush.pop());
            }
        }
    }

    public void add(int newNum){
        this.stackPush.push(newNum);
        this.pushToPop();
    }

    public int poll(){
        if(this.stackPush.isEmpty() && this.stackPop.isEmpty()){
            throw new RuntimeException("Your queue is empty");
        }
        this.pushToPop();
        return this.stackPop.pop();
    }

    public int peek(){
        if(this.stackPush.isEmpty() && this.stackPop.isEmpty()){
            throw new RuntimeException("Your queue is empty");
        }
        this.pushToPop();
        return this.stackPop.peek();
    }
}
